package com.yuejian_06.yuejian_06.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.yuejian_06.yuejian_06.Listener.LoginListener;
import com.yuejian_06.yuejian_06.R;
import com.yuejian_06.yuejian_06.bean.CourseBean;
import com.yuejian_06.yuejian_06.bean.SuccessBean;
import com.yuejian_06.yuejian_06.model.CourseModel;

public class OrderingActivity extends  BaseActivity {
    TextView order_courseName,order_price;
    RatingBar order_courseStar;
    EditText order_userName,order_ID,order_userTele;
    Button order_btn;
    ImageButton order_back;
    String course_id,sum_price;


    private LoginListener<CourseBean> Listener=new LoginListener<CourseBean>() {
        @Override
        public void onResponse(CourseBean o) {
            CourseBean mDataList=o;
            sum_price=mDataList.getPrice().toString();
            order_courseName.setText(mDataList.getName());
            order_price.setText(mDataList.getPrice()+"元");
            order_courseStar.setRating(Integer.valueOf(mDataList.getStar()));
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(getApplicationContext(),"失败了"+course_id,Toast.LENGTH_SHORT).show();
        }
    };

    private LoginListener<SuccessBean> Listener2=new LoginListener<SuccessBean>() {
        @Override
        public void onResponse(SuccessBean o) {
            SuccessBean mDataList=o;
            String success=mDataList.getSuccess();
            if ("0".equals(success))
            {//处理失败
                Toast.makeText(OrderingActivity.this,"下单失败",Toast.LENGTH_SHORT).show();
            }
            else{
                finish();
                Toast.makeText(OrderingActivity.this,"下单成功",Toast.LENGTH_LONG).show();
                Intent intent=new Intent(OrderingActivity.this, UploadSuccessActivity.class);
                startActivity(intent);
            }
        }
        @Override
        public void onFail(String msg) {
            Toast.makeText(getApplicationContext(),"失败了"+course_id,Toast.LENGTH_SHORT).show();
        }
    };



    @Override
    void initViews() {
        setLayout(R.layout.activity_ordering);
        order_courseName=(TextView)findViewById(R.id.order_courseName);
        order_price=(TextView)findViewById(R.id.order_price);
        order_courseStar=(RatingBar)findViewById(R.id.order_courseStar);
        order_userName=(EditText)findViewById(R.id.order_userName);
        order_ID=(EditText)findViewById(R.id.order_ID);
        order_userTele=(EditText)findViewById(R.id.order_userTele);
        order_btn=(Button) findViewById(R.id.order_btn);
        order_back=(ImageButton)findViewById(R.id.order_back);
        Bundle bundle = getIntent().getExtras();
        course_id = bundle.getString("course_id");
    }

    @Override
    void initEvents() {
        order_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CourseModel courseModel=new CourseModel();
                courseModel.courseOrder(getUser_id(),
                        course_id,
                        order_userName.getText().toString(),
                        order_ID.getText().toString(),
                        sum_price,
                        order_userTele.getText().toString(),
                        Listener2);
            }
        });

        order_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    void initData() {
        CourseModel courseModel=new CourseModel();
        courseModel.courseInfo(course_id,Listener);

    }
}
